/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.dao.jpa;

import com.wii.elpro.dao.InqueryDAOInterface;
import com.wii.elpro.dao.JPAUtil;
import com.wii.elpro.entity.Cabang;
import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.Project;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author hendro
 */
public class InqueryDAOImpl extends DAOGenericImpl implements InqueryDAOInterface{

    public Inquery getInquery(long id) throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.find(Inquery.class, id);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries() throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.createQuery("SELECT p FROM Inquery p").getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries(Cabang cab) throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.createQuery("SELECT p FROM Inquery p WHERE p.cabang=:input")
                    .setParameter(0,cab)
                    .getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries(Project project) throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.createQuery("SELECT p FROM Inquery p WHERE p.project=:input")
                    .setParameter(0,project)
                    .getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public Inquery getInquery(String noQuotation) throws Exception {
         JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return (Inquery)em.createQuery("SELECT p FROM Inquery p WHERE p.noQuotation=:input")
                    .setParameter(0,noQuotation)
                    .getSingleResult();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries(Project project, Cabang cab) throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.createQuery("SELECT p FROM Inquery p WHERE p.project=:input1 AND p.cabang=:input2")
                    .setParameter(0,project)
                    .setParameter(1, cab)
                    .getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }
    
    public Inquery getLastInquiry() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
